<?php
/*
Template Name: sendBuyingLeads
Author: NajeebMedia.com
*/
?>
<?php get_header(); ?>

<?php
// Get the IDs for posts with both meta keys and values
if(isset($_POST['advertise_btn']))
{
	$mailer_path = dirname(__FILE__)."/lib/class.MailSender.php";
	include($mailer_path);
	$mailer = new MyMailer();
	
	$send_email = dirname(__FILE__).'/emails/advertise-request.php';
	include($send_email);
	?>
    <div class="hentry-meta">
        <h1>Your message has be sent successfully, we will contact soon</h1>
	</div>
<?php	
}

elseif(isset($_POST['buying_btn']))
{
	
$posts_with_meta = $wpdb->get_col("
	SELECT ID from $wpdb->posts p
		JOIN $wpdb->postmeta meta1 ON meta1.post_id = p.ID
		WHERE p.post_status = 'publish'
		AND p.post_type = 'post'
		AND meta1.meta_key = 'company_product_ids'
		AND meta1.meta_value = ".$_POST['product_id']);
		
		
// Create query object
$the_query = new WP_Query();
// Setup query paramters
$the_query->query( array( 'post__in' => $posts_with_meta ) );

$mailer_path = dirname(__FILE__)."/lib/class.MailSender.php";
include($mailer_path);
$mailer = new MyMailer();

// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();

	$email = get_post_meta($post -> ID, 'emailAddress', true);
	
	$send_email = dirname(__FILE__).'/emails/buying-leads.php';
	include($send_email);

endwhile;
// Reset Post Data
wp_reset_postdata();
?>
<div class="hentry-meta">
        <h1>Your enquiry has be sent successfully to relavant companies</h1>
</div>
<?php
}
?>


<?php get_footer(); ?>
